<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>信息修改申请管理</title>
    <link rel="stylesheet" href="css/adminUpdateRequest.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="main-wrapper">
    <div class="sidebar">
        <h3>医院病例管理系统</h3>
        <ul>
            <li><a href="./adminUserList.html">用户管理</a></li>
            <li><a href="./adminMedicalRecordList.html">病历管理</a></li>
            <li><a href="./adminPersonalCenter.html">个人信息管理</a></li>
            <li><a href="./adminMedicalTemplate.html">病历模板管理</a></li>
            <li><a href="./adminUpdateRequest.html" class="active">信息修改申请</a></li>
        </ul>
    </div>
    <div class="main-content">
        <div class="info-card">
            <h2>信息修改申请管理</h2>
            <table class="data-table">
                <thead>
                <tr>
                    <th>申请ID</th>
                    <th>用户ID</th>
                    <th>字段</th>
                    <th>原值</th>
                    <th>新值</th>
                    <th>申请时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="requestTableBody">
                </tbody>
            </table>
            <div id="loading" class="loading">加载中...</div>
            <div id="error" class="alert error" style="display: none;">加载失败</div>
        </div>
    </div>
</div>
<script>
$(function(){
    const backendBaseUrl = 'http://localhost:8080';

    function loadRequests() {
        $('#loading').show();
        $('#error').hide();
        $.ajax({
            url: backendBaseUrl + '/admin/updateRequest/list',
            type: 'GET',
            xhrFields: { withCredentials: true },
            success: function(res) {
                $('#loading').hide();
                if(res.code === 0 && res.data) {
                    const $tbody = $('#requestTableBody');
                    $tbody.empty();
                    if (res.data.length === 0) {
                        $tbody.append('<tr><td colspan="7" class="text-center">暂无待审核申请</td></tr>');
                        return;
                    }
                    res.data.forEach(req => {
                        const fieldLabel = req.fieldName === 'history' ? '既往史' : (req.fieldName === 'allergy' ? '过敏史' : req.fieldName);
                        const $row = $('<tr>');
                        $row.append($('<td>').text(req.id));
                        $row.append($('<td>').text(req.userId));
                        $row.append($('<td>').text(fieldLabel));
                        $row.append($('<td>').text(req.oldValue || '-'));
                        $row.append($('<td>').text(req.newValue || '-'));
                        $row.append($('<td>').text(req.createTime ? new Date(req.createTime).toLocaleString() : '-'));
                        const $approveBtn = $('<button>').addClass('btn btn-success approve-btn').text('通过').attr('data-id', req.id);
                        const $rejectBtn = $('<button>').addClass('btn btn-danger reject-btn').text('拒绝').attr('data-id', req.id);
                        const $actionsCell = $('<td>').append($approveBtn, ' ', $rejectBtn); // Add space between buttons
                        $row.append($actionsCell);
                        $tbody.append($row);
                    });
                } else {
                    $('#error').text(res.message || '获取申请列表失败').show();
                }
            },
            error: function(){
                $('#loading').hide();
                $('#error').text('请求失败，请稍后重试').show();
            }
        });
    }
    // 审核通过
    $('#requestTableBody').on('click', '.approve-btn', function(){
        const id = $(this).data('id');
        if (confirm('确定通过该申请吗？')) {
            $.ajax({
                url: backendBaseUrl + '/admin/updateRequest/review',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({id: id, status: 1}),
                 xhrFields: { withCredentials: true },
                success: function(res) {
                    if (res.code === 0) {
                        alert('操作成功');
                        loadRequests();
                    } else {
                        alert(res.message || '操作失败');
                    }
                },
                 error: function(){
                    alert('操作失败，请稍后重试');
                }
            });
        }
    });
    // 审核拒绝
    $('#requestTableBody').on('click', '.reject-btn', function(){
        const id = $(this).data('id');
        if (confirm('确定拒绝该申请吗？')) {
            $.ajax({
                url: backendBaseUrl + '/admin/updateRequest/review',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({id: id, status: 2}),
                 xhrFields: { withCredentials: true },
                success: function(res) {
                    if (res.code === 0) {
                        alert('操作成功');
                        loadRequests();
                    } else {
                        alert(res.message || '操作失败');
                    }
                },
                 error: function(){
                    alert('操作失败，请稍后重试');
                }
            });
        }
    });
    loadRequests();
});
</script>
</body>
</html> 